package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.NganhM;
import net.sf.json.JSONObject;
import dao.NganhD;

@WebServlet("/NganhServlet")
public class NganhServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private NganhD dao;
       
    /**
     * @function constructor NganhServlet
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public NganhServlet() throws Exception {
        super();
        this.dao = new NganhD();
    }

    /**
	 * Destruction of the servlet.
	 */
	public void destroy() {
		super.destroy();
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		
		String flag = request.getParameter("flag");
		try {
			if (flag == null || "".equals(flag.trim())) {
				showNganh(request, response);
			}else if ("add".equals(flag.trim())) {
				String ma = request.getParameter("ma");
				String ten = request.getParameter("ten");
				String id_bac_dao_tao = request.getParameter("id_bac_dao_tao");
				String id_don_vi_ql = request.getParameter("id_don_vi_ql");
				addNganh(ma,ten,Integer.parseInt(id_bac_dao_tao),Integer.parseInt(id_don_vi_ql),request,response);
			}else if ("edit".equals(flag.trim())) {
				String id = request.getParameter("id_nganh");
				String ma = request.getParameter("ma");
				String ten = request.getParameter("ten");
				String id_bac_dao_tao = request.getParameter("id_bac_dao_tao");
				String id_don_vi_ql = request.getParameter("id_don_vi_ql");
				editNganh(Integer.parseInt(id),ma,ten,Integer.parseInt(id_bac_dao_tao),Integer.parseInt(id_don_vi_ql), request, response);
			}else if ("remove".equals(flag.trim())) {
				String[] id = request.getParameter("id").split(",");
				for(int i=0; i<id.length; i++){
					deleteNganh(Integer.parseInt(id[i]), request, response);	
				}		
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @function show Nganh
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void showNganh(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String pageCount = request.getParameter("page");
		String rowsCount = request.getParameter("rows");
		String search=request.getParameter("searchTen")==null?"":request.getParameter("searchTen");
		
		int page = (pageCount == null || "".equals(pageCount)) ? 1 : Integer.valueOf(pageCount);
		int rows = (rowsCount == null || "".equals(rowsCount)) ? 10 : Integer.valueOf(rowsCount);
		
		int offSet = (page - 1) * rows;
		int allCount = dao.searchNganh(search).size();
		
		returnMap.put("total", allCount);
		returnMap.put("rows", dao.getAlls(search, offSet, rows));
		
		String returnString = JSONObject.fromObject(returnMap).toString();
		PrintWriter out = response.getWriter();
		out.println(returnString);
		out.flush();
		out.close();
	}
	
	/**
	 * @function add Nganh
	 * @param ma
	 * @param ten
	 * @param id_bac_dao_tao
	 * @param id_don_vi_ql
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void addNganh(String ma, String ten, int id_bac_dao_tao, int id_don_vi_ql, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		NganhM n = new NganhM();
		n.setMa(ma);
		n.setTen(ten);
		n.setId_bac_dao_tao(id_bac_dao_tao);
		n.setId_don_vi_ql(id_don_vi_ql);
		if(dao.Insert(n)==true){
			map.put("success", "true");
			map.put("msg", "Thêm thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi thêm dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function edit Nganh
	 * @param id_nganh
	 * @param ma
	 * @param ten
	 * @param id_bac_dao_tao
	 * @param id_don_vi_ql
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void editNganh(int id_nganh, String ma, String ten, int id_bac_dao_tao, int id_don_vi_ql, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		NganhM n = new NganhM();
		n.setId_nganh(id_nganh);
		n.setMa(ma);
		n.setTen(ten);
		n.setId_bac_dao_tao(id_bac_dao_tao);
		n.setId_don_vi_ql(id_don_vi_ql);
		if(dao.Update(n)==true){
			map.put("success", "true");
			map.put("msg", "Sửa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi sửa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
	
	/**
	 * @function delete Nganh
	 * @param id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	private void deleteNganh(int id, HttpServletRequest request,HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if(dao.Delete(id)==true){
			map.put("success", "true");
			map.put("msg", "Xóa thành công");
		} else {
			map.put("success", "false");
			map.put("msg", "Lỗi xóa dữ liệu");
		}
		String returnFlag = JSONObject.fromObject(map).toString();
		PrintWriter out = response.getWriter();
		out.println(returnFlag);
		out.flush();
		out.close();
	}
}
